package cn.owen.config;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * shiro拦截器配置
 * @author fanqinglong
 *
 */
@Configuration
public class ShiroConfig {
	@Bean
	public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		shiroFilterFactoryBean.setLoginUrl("/admin/login");
		Map<String, String> filterChainMap = new LinkedHashMap<>();
		filterChainMap.put("/statics/**", "anon");
		filterChainMap.put("/admin/login", "anon");
		filterChainMap.put("/admin/login.htm", "anon");
		filterChainMap.put("admin/loginout", "logout");
		filterChainMap.put("/**", "authc");
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);
		return shiroFilterFactoryBean;
	}
	
	@Bean
	public SecurityManager securityManager(){
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		Authenticator authenticator = new CustomizedModularRealmAuthenticator();
		securityManager.setAuthenticator(authenticator);
		List<Realm> realms = new ArrayList<>();
		realms.add(userRealm());
		//realms.add(accountRealm());
		securityManager.setRealms(realms);
		return securityManager;
	}
	
	@Bean
	public UserRealm userRealm(){
		return new UserRealm();
	}
	
	/*@Bean
	public AccountRealm accountRealm(){
		return new AccountRealm();
	}*/
}
